﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DoAnForum.Models;

namespace DoAnForum.Controllers
{
    public class ShareController : Controller
    {
        //
        // GET: /Share/
        private static DoAnForumEntities db = new DoAnForumEntities();

        public ActionResult ShowTopic(int id)
        {
            var bv = new TopicViewModel();
            bv.Topic = db.Topics.SingleOrDefault(x => x.TopicId == id);
            bv.SumThanked = db.Comments.Where(x => x.TopicId == id).Select(x => x.Thanks.Count).Sum();
            bv.FirstComment = db.Comments.SingleOrDefault(x => x.TopicId == id && x.IsStartTopic);
            bv.CreateUser = db.Users.SingleOrDefault(x => x.UserId == bv.Topic.UserId);
            bv.ACreateUser = db.aspnet_Users.SingleOrDefault(x => x.UserId == bv.Topic.UserId);
            var lastMessageId = bv.Topic.Comments.Select(x => x.CommentId).Max();
            var temp = db.Comments.SingleOrDefault(x => x.CommentId == lastMessageId);
            bv.LastCommentUser = db.Users.SingleOrDefault(x => x.UserId == temp.UserId);
            bv.ALastCommentUser = db.aspnet_Users.SingleOrDefault(x => x.UserId == temp.UserId);
            bv.LastDate = temp.CommentCreationDate;
            return PartialView("ShowTopic",bv);
        }

        public ActionResult ShowComment(int id)
        {
            var cm = new ShowTopicModel();
            cm.Comment = db.Comments.SingleOrDefault(x => x.CommentId == id);
            cm.User = db.Users.SingleOrDefault(x => x.UserId == cm.Comment.UserId);
            cm.Auser = db.aspnet_Users.SingleOrDefault(x => x.UserId == cm.Comment.UserId);
            cm.LastEditUser = db.aspnet_Users.SingleOrDefault(x => x.UserId == cm.Comment.CommentLastEditUser);
            cm.NoThank = db.Thanks.Count(x => x.CommentID == id);
            cm.NoPost = db.Comments.Count(x => x.UserId == cm.Comment.UserId);
            var singleOrDefault = db.Comments.SingleOrDefault(x => x.CommentId == id);
            if (singleOrDefault != null)
            {
                var comment = singleOrDefault.Thanks;
                var sampleData = (comment.Join(db.aspnet_Users, tab1 => tab1.ThankFromUserId, tab2 => tab2.UserId,
                                               (tab1, tab2) => new
                                                                   {
                                                                       tab2.UserName
                                                                   })
                                 ).ToList();
                var userThanked = sampleData.Select(x => x.UserName.ToUpper()).ToList();
                ViewBag.userThanked = userThanked;
            }
            else
            {
                ViewBag.userThanked = null;
            }
            return PartialView("ShowComment", cm);
        }

        
        public ActionResult TagBarPartial(int id)
        {
            var topic = db.Topics.SingleOrDefault(x => x.TopicId == id);
            var tag = topic.Tags;
            return PartialView("TagBarPartial", tag);
        }

        public ActionResult TagCloundPartial()
        {
            var tag = db.Tags.ToList().OrderByDescending(x => x.Topics.Count).Take(20);
            return PartialView("TagCloundPartial", tag);
        }

        public ActionResult BoxCategory()
        {
            var category = db.Categories.Where(x=>x.Level==1);
            return PartialView("BoxCategory", category);
        }

        public ActionResult TopNew()
        {
            var cm = db.Topics.Where(x => x.Active).OrderBy(x=>x.TopicCreationDate).Take(10);
            return PartialView("TopNew", cm);
        }

        [AccessDeniedAuthorize(Roles = "Admin, Mod, Member")]
        public ActionResult ThankUp(int commentId, String crrName)
        {
            ThankBuss(commentId,crrName);
            int noThank = db.Comments.SingleOrDefault(x => x.CommentId == commentId).Thanks.Count;
            ViewBag.NoThank = noThank;
            return PartialView("getVote");
        }

        private void ThankBuss(int commentId, String crrName)
        {
            var tks = new Thank();
            tks.CommentID = commentId;
            tks.ThanksDate = DateTime.Today;
            var crrUser = db.aspnet_Users.SingleOrDefault(x => crrName.Equals(x.UserName));
            tks.ThankFromUserId = crrUser.UserId;
            db.Thanks.AddObject(tks);
            db.SaveChanges();
        }

        public ActionResult BoxProfile()
        {
            var crrUser = db.aspnet_Users.SingleOrDefault(x => User.Identity.Name.Equals(x.UserName));
            var check = db.PMessages.Count(x => (x.ToUserId == crrUser.UserId) && x.Flags);
            if (check>0)
            {
                ViewBag.UnreadMessage = check;
            }
            else
            {
                ViewBag.UnreadMessage = null;
            }
            return PartialView("BoxProfile");
        }
    }
}
